/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.games.epkb.controller;

import it.polimi.games.epkb.controller.exceptions.NonexistentEntityException;
import it.polimi.games.epkb.controller.exceptions.PreexistingEntityException;
import it.polimi.games.epkb.entityClasses.ProcessStructure;
import it.polimi.games.epkb.entityClasses.ProcessStructurePK;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import it.polimi.games.epkb.entityClasses.BusinessProcess;
import it.polimi.games.epkb.entityClasses.Activity;
import javax.transaction.UserTransaction;

/**
 *
 * @author plebani
 */
public class ProcessStructureJpaController implements Serializable {

    public ProcessStructureJpaController(EntityManagerFactory emf) {
        this.utx = null;
        this.emf = emf;
    }

    public ProcessStructureJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ProcessStructure processStructure) throws PreexistingEntityException, Exception {
        if (processStructure.getProcessStructurePK() == null) {
            processStructure.setProcessStructurePK(new ProcessStructurePK());
        }
        processStructure.getProcessStructurePK().setIdThisActivity(processStructure.getActivity2().getId());
        processStructure.getProcessStructurePK().setIDActivityFOLLOW(processStructure.getActivity1().getId());
        processStructure.getProcessStructurePK().setIDActivityPREC(processStructure.getActivity().getId());
        processStructure.getProcessStructurePK().setIdProcess(processStructure.getBusinessProcess().getId());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            BusinessProcess businessProcess = processStructure.getBusinessProcess();
            if (businessProcess != null) {
                businessProcess = em.getReference(businessProcess.getClass(), businessProcess.getId());
                processStructure.setBusinessProcess(businessProcess);
            }
            Activity activity = processStructure.getActivity();
            if (activity != null) {
                activity = em.getReference(activity.getClass(), activity.getId());
                processStructure.setActivity(activity);
            }
            Activity activity1 = processStructure.getActivity1();
            if (activity1 != null) {
                activity1 = em.getReference(activity1.getClass(), activity1.getId());
                processStructure.setActivity1(activity1);
            }
            Activity activity2 = processStructure.getActivity2();
            if (activity2 != null) {
                activity2 = em.getReference(activity2.getClass(), activity2.getId());
                processStructure.setActivity2(activity2);
            }
            em.persist(processStructure);
            if (businessProcess != null) {
                businessProcess.getProcessStructureCollection().add(processStructure);
                businessProcess = em.merge(businessProcess);
            }
            if (activity != null) {
                activity.getProcessStructureCollection().add(processStructure);
                activity = em.merge(activity);
            }
            if (activity1 != null) {
                activity1.getProcessStructureCollection().add(processStructure);
                activity1 = em.merge(activity1);
            }
            if (activity2 != null) {
                activity2.getProcessStructureCollection().add(processStructure);
                activity2 = em.merge(activity2);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findProcessStructure(processStructure.getProcessStructurePK()) != null) {
                throw new PreexistingEntityException("ProcessStructure " + processStructure + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ProcessStructure processStructure) throws NonexistentEntityException, Exception {
        processStructure.getProcessStructurePK().setIdThisActivity(processStructure.getActivity2().getId());
        processStructure.getProcessStructurePK().setIDActivityFOLLOW(processStructure.getActivity1().getId());
        processStructure.getProcessStructurePK().setIDActivityPREC(processStructure.getActivity().getId());
        processStructure.getProcessStructurePK().setIdProcess(processStructure.getBusinessProcess().getId());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ProcessStructure persistentProcessStructure = em.find(ProcessStructure.class, processStructure.getProcessStructurePK());
            BusinessProcess businessProcessOld = persistentProcessStructure.getBusinessProcess();
            BusinessProcess businessProcessNew = processStructure.getBusinessProcess();
            Activity activityOld = persistentProcessStructure.getActivity();
            Activity activityNew = processStructure.getActivity();
            Activity activity1Old = persistentProcessStructure.getActivity1();
            Activity activity1New = processStructure.getActivity1();
            Activity activity2Old = persistentProcessStructure.getActivity2();
            Activity activity2New = processStructure.getActivity2();
            if (businessProcessNew != null) {
                businessProcessNew = em.getReference(businessProcessNew.getClass(), businessProcessNew.getId());
                processStructure.setBusinessProcess(businessProcessNew);
            }
            if (activityNew != null) {
                activityNew = em.getReference(activityNew.getClass(), activityNew.getId());
                processStructure.setActivity(activityNew);
            }
            if (activity1New != null) {
                activity1New = em.getReference(activity1New.getClass(), activity1New.getId());
                processStructure.setActivity1(activity1New);
            }
            if (activity2New != null) {
                activity2New = em.getReference(activity2New.getClass(), activity2New.getId());
                processStructure.setActivity2(activity2New);
            }
            processStructure = em.merge(processStructure);
            if (businessProcessOld != null && !businessProcessOld.equals(businessProcessNew)) {
                businessProcessOld.getProcessStructureCollection().remove(processStructure);
                businessProcessOld = em.merge(businessProcessOld);
            }
            if (businessProcessNew != null && !businessProcessNew.equals(businessProcessOld)) {
                businessProcessNew.getProcessStructureCollection().add(processStructure);
                businessProcessNew = em.merge(businessProcessNew);
            }
            if (activityOld != null && !activityOld.equals(activityNew)) {
                activityOld.getProcessStructureCollection().remove(processStructure);
                activityOld = em.merge(activityOld);
            }
            if (activityNew != null && !activityNew.equals(activityOld)) {
                activityNew.getProcessStructureCollection().add(processStructure);
                activityNew = em.merge(activityNew);
            }
            if (activity1Old != null && !activity1Old.equals(activity1New)) {
                activity1Old.getProcessStructureCollection().remove(processStructure);
                activity1Old = em.merge(activity1Old);
            }
            if (activity1New != null && !activity1New.equals(activity1Old)) {
                activity1New.getProcessStructureCollection().add(processStructure);
                activity1New = em.merge(activity1New);
            }
            if (activity2Old != null && !activity2Old.equals(activity2New)) {
                activity2Old.getProcessStructureCollection().remove(processStructure);
                activity2Old = em.merge(activity2Old);
            }
            if (activity2New != null && !activity2New.equals(activity2Old)) {
                activity2New.getProcessStructureCollection().add(processStructure);
                activity2New = em.merge(activity2New);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                ProcessStructurePK id = processStructure.getProcessStructurePK();
                if (findProcessStructure(id) == null) {
                    throw new NonexistentEntityException("The processStructure with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(ProcessStructurePK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ProcessStructure processStructure;
            try {
                processStructure = em.getReference(ProcessStructure.class, id);
                processStructure.getProcessStructurePK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The processStructure with id " + id + " no longer exists.", enfe);
            }
            BusinessProcess businessProcess = processStructure.getBusinessProcess();
            if (businessProcess != null) {
                businessProcess.getProcessStructureCollection().remove(processStructure);
                businessProcess = em.merge(businessProcess);
            }
            Activity activity = processStructure.getActivity();
            if (activity != null) {
                activity.getProcessStructureCollection().remove(processStructure);
                activity = em.merge(activity);
            }
            Activity activity1 = processStructure.getActivity1();
            if (activity1 != null) {
                activity1.getProcessStructureCollection().remove(processStructure);
                activity1 = em.merge(activity1);
            }
            Activity activity2 = processStructure.getActivity2();
            if (activity2 != null) {
                activity2.getProcessStructureCollection().remove(processStructure);
                activity2 = em.merge(activity2);
            }
            em.remove(processStructure);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ProcessStructure> findProcessStructureEntities() {
        return findProcessStructureEntities(true, -1, -1);
    }

    public List<ProcessStructure> findProcessStructureEntities(int maxResults, int firstResult) {
        return findProcessStructureEntities(false, maxResults, firstResult);
    }

    private List<ProcessStructure> findProcessStructureEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ProcessStructure.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ProcessStructure findProcessStructure(ProcessStructurePK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ProcessStructure.class, id);
        } finally {
            em.close();
        }
    }

    public int getProcessStructureCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ProcessStructure> rt = cq.from(ProcessStructure.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
